<template>
  <div id="user-list">
    <div class="user-list-wrap">
      <!-- 用户管理标题 -->
      <p class="user-list-title">用户管理</p>
      <div class="articleAdd-wrap">
        <el-button type="primary" @click="userAdd">添加用户</el-button>
      </div>
      <div class="user-list-wrap">
        <!-- 用户展示 -->
        <el-table ref="singleTable" :data="userList" highlight-current-row style="width: 100%">
          <el-table-column property="id" label="ID" width="250"></el-table-column>
          <el-table-column property="username" label="账号"></el-table-column>
          <el-table-column property="realname" label="用户名称"></el-table-column>
          <!-- 用户操作 -->
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="editUser(scope.row)" type="text" size="small">编辑</el-button>
              <el-button type="text" size="small" @click="delUser(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="user-paging-wrap">
          <div class="user-paging-left">
            <span>共 {{userList.length}} 条</span> /
            <span>共{{total}}页</span> /
            <span>第1页</span>
          </div>
          <el-pagination background layout="prev, pager, next" :total="userList.length"></el-pagination>
        </div>
      </div>
    </div>
    <!-- 用户添加编辑表单 -->
    <el-dialog title="用户添加" :visible.sync="dialogFormVisible">
      <!-- 用户名 -->
      <el-form :model="useradd" :rules="switchs?rules:rulese" ref="ruleForm">
        <el-form-item label="用户名" label-width="100px" prop="realname">
          <el-input v-model="useradd.realname" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 用户账号 -->
        <el-form-item label="用户账号" label-width="100px" prop="username">
          <el-input v-model="useradd.username" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 用户密码 -->
        <el-form-item label="用户密码" label-width="100px" prop="password">
          <el-input v-model="useradd.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userList: [],
      total: 0,
      // 表单蒙版
      dialogFormVisible: false,
      switchs: false,
      // 用户添加数据
      useradd: {
        realname: "",
        username: "",
        password: ""
      },
      // 表单验证规则
      rules: {
        realname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 11, message: "长度在 3 到 11 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 11, message: "长度在 6 到 11 个字符", trigger: "blur" }
        ]
      },
      rulese: {}
    };
  },
  methods: {
    // 获取用户列表
    getUserList() {
      this.$http.get("api/login/userlist").then(res => {
        this.userList = res.data.userList;
        this.total = res.data.total;
      });
    },
    // 删除用户
    delUser(id) {
      this.$http({
        url: "api/login/delUser",
        method: "post",
        data: {
          id
        }
      }).then(res => {
        // 判断是否删除成功
        if (res.data.code === 1) {
          this.$message({
            message: res.data.msg,
            type: "success"
          });
          // 删除后重新渲染用户列表
          this.getUserList();
        } else {
          this.$message({
            message: res.data.msg,
            type: "warning"
          });
        }
      });
    },
    // 添加用户事件
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.switchs) {
            this.userAddRequest();
          } else {
            this.editUserRequest();
          }
          this.dialogFormVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 取消表单
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    // 用户添加请求
    userAddRequest() {
      this.$http({
        url: "api/login/userAdd",
        method: "post",
        data: {
          ...this.useradd
        }
      }).then(res => {
        this.Request(res)
      });
    },
    // 用户添加清空表单数据
    userAdd() {
      this.switchs = true;
      this.dialogFormVisible = true;
      // this.$refs["ruleForm"].resetFields();
      // 延时清空，在打开表单蒙版一瞬间无法清除数据，需要延时清空
      this.useradd = {
        realname: "",
        username: "",
        password: ""
      };
      setTimeout(() => {
        this.$refs["ruleForm"].resetFields();
      }, 0);
    },
    // 用户编辑
    editUser(row) {
      this.switchs = false;
      this.dialogFormVisible = true;
      this.useradd = {
        ...row
      };
    },
    editUserRequest() {
      this.$http({
        url: "api/login/editUser",
        method: "post",
        data: {
          ...this.useradd
        }
      }).then(res => {
        this.Request(res)
      });
    },
    // 请求成功弹窗
    Request(res) {
      // 判断是否添加成功
      if (res.data.code === 1) {
        this.$message({
          message: res.data.msg,
          type: "success"
        });
        // 添加成功重新调用用户列表
        this.getUserList();
      } else {
        // 失败弹窗
        this.$message({
          message: res.data.msg,
          type: "warning"
        });
      }
    }
  },
  mounted() {
    this.getUserList();
  },
  watch: {
    dialogFormVisible(newVal) {
      if (newVal === false) {
        this.$refs["ruleForm"].resetFields();
      }
    }
  }
};
</script>
<style lang='less'>
#user-list {
  // 用户管理盒子
  .user-list-wrap {
    // 文章列表标题
    .user-list-title {
      margin: 0;
      padding: 0;
      height: 40px;
      padding-left: 30px;
      color: #fff;
      background: #0078ff;
      line-height: 40px;
      border-top-right-radius: 5px;
      border-top-left-radius: 5px;
    }
    .articleAdd-wrap {
      height: 70px;
      line-height: 70px;
      padding-left: 30px;
    }
    // 文章列表盒子
    .user-list-wrap {
      padding: 0 30px;
      //   分页
      .user-paging-wrap {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 30px;
      }
    }
  }
}
</style>